package edu.rit.drh4818.fire.data;

import java.util.Random;


/**
 * First attempt at flame data that expands.
 * 
 * @author Darren Headrick
 *
 */
public class ExpandingFlameData extends EmitterVolume
{

	private static final int DIMENSION = 50;
	public static String NAME = "Expanding";
	
	public ExpandingFlameData()
	{
		super( DIMENSION, true, NAME );
	}
	
	/*
	 * (non-Javadoc)
	 * @see edu.rit.drh4818.volume.renderer.VolData#update()
	 */
	public void update() 
	{
		if( volData == null ) return;
		
		Random r = new Random();
		
		short[][][] tmp = new short[ DIMENSION ][ DIMENSION ][ DIMENSION ];
		
		for( int y = 0; y < DIMENSION - 1; y++ )
		{
			for( int x = 0; x < DIMENSION; x++ )
			{
				for( int z = 0; z < DIMENSION; z++ )
				{
					if( volData[x][y][z] != 0 )
					{
						int xoff = r.nextInt( 3 ) - 1, zoff = r.nextInt( 3 ) - 1;
						
						if( x + xoff < 0 || x + xoff >= DIMENSION ) xoff = 0;
						if( z + zoff < 0 || z + zoff >= DIMENSION ) zoff = 0; 
						
						if( tmp[x+xoff][y+1][z+zoff] != 0 )
						{
							tmp[x+xoff][y+1][z+zoff] = 
								(byte)Math.min(volData[x][y][z] + r.nextInt(40),
										tmp[x+xoff][y+1][z+zoff] );
							if( tmp[x+xoff][y+1][z+zoff] < 0 ) tmp[x+xoff][y+1][z+zoff] = 0;
						}
						else
						{
							tmp[x+xoff][y+1][z+zoff] = (short)(volData[x][y][z] + r.nextInt(25));
						}
						if( tmp[x+xoff][y+1][z+zoff] > 255 ) tmp[x+xoff][y+1][z+zoff] = 0;
					}
				}
			}
		}
		
		volData = tmp;
		super.update();

		generateTexture();
	}
}
